/////////////////////////////////////////////////////////////////////////////
// This was supposed to be the solution to 11.2 description of implementation
// Oh my... nvm
/////////////////////////////////////////////////////////////////////////////
// Implemented reloadDataAndChangeCount
// //////////////////////////////////////////////////////////////////////////

#import "MyDocument.h"

@implementation MyDocument


- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
	return [shoppingListArray count];
}


- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:
(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
	return [shoppingListArray objectAtIndex:rowIndex];
}


- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:
(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
    [shoppingListArray replaceObjectAtIndex:rowIndex withObject:anObject];
    
    [self reloadDataAndChangeCount:shoppingListTableView];
}


- (IBAction) addNewItemToShoppingList:(id)sender
{
    [shoppingListArray addObject:
     [newItemNameTextField stringValue]];
    
    [newItemNameTextField setStringValue:@""];
    
    [self reloadDataAndChangeCount:shoppingListTableView];

}


- (IBAction) rmExistingItemFromShoppingList:(id)sender
{
    if([shoppingListTableView selectedRow] != -1){
        
        [shoppingListArray removeObjectAtIndex:[shoppingListTableView selectedRow]];
        
        [self reloadDataAndChangeCount:shoppingListTableView];
    }
}


- (void) reloadDataAndChangeCount:(NSTableView *)table  
{
    [table reloadData];
    
    //[self updateChangeCount:1];
}


- (id)init
{
    self = [super init];
    if (self) {
		
		shoppingListArray = [[NSMutableArray alloc] initWithObjects:
							 @"Milk", @"Eggs", @"Butter", nil];
    
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self release] message and return nil.
    
    }
    return self;
}


- (void) dealloc
{
	[shoppingListArray release];
	
	[super dealloc];
}


- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
    return [shoppingListArray writeToURL:absoluteURL atomically:YES];
}


- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
    [shoppingListArray release];
    
    shoppingListArray = [[NSMutableArray alloc] initWithContentsOfURL:absoluteURL];
    
    [shoppingListTableView reloadData];
    
    return YES;
}


- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, 
	// you should remove this method and override -makeWindowControllers instead.
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}


- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    // Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.

    // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.

    // For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:.
	// In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.

    if ( outError != NULL ) {
		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
	}
	return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    // Insert code here to read your document from the given data of the specified type.  If the given outError != NULL, ensure that you set *outError when returning NO.

    // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. 
    
    // For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType.
	// In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
    
    if ( outError != NULL ) {
		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
	}
    return YES;
}

@end
